const { LinValidator, Rule } = require('./lin-validator')
const { User } = require('@/app/models/user')
class PositiveIntegerValidator extends LinValidator{
  constructor(){
    super()
    //如果要使用this，必须需要先调用super()
    this.id = [
      //第一个参数:规则，第二个参数是提示语，第三个参数是可选参数
      new Rule('isInt','需要是正整数',{min:1})
    ]
  }
}
//注册接口参数校验
class RegisterValidator extends LinValidator{
  constructor(){
    super()
    
    this.email = [
      new Rule('isEmail','不符合Email规范')
    ]
    this.password = [
      new Rule('isLength','密码长度6-20字符',{
        min: 6,
        max: 20
      }),
      new Rule('matches','密码不符合规则','^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{6,20}$')
    ]
    this.password2 = this.password
    this.nickname = [
      new Rule('isLength','昵称长度2-32字符',{
        min: 2,
        max: 32
      }),
    ]
  }
  validatePassword(vals){
    const psw1 = vals.body.password
    const psw2 = vals.body.password2
    if(psw1 !== psw2){
      throw new Error('两个密码不同')
    }
  }
  
  async validateEmail(vals){
    const email = vals.body.email
    const user = await User.findOne({
      where:{
        email
      }
    })
    if(user){
      throw new Error('email已存在')
    }
  }
}
//login参数校验
class LoginValidator extends LinValidator{
  constructor(){
    super()
    //账号校验
    this.account = [
      new Rule('isEmail','不符合Email规范')
    ]
    //密码校验 
    this.password = [
      new Rule('isOptional'),
      new Rule('isLength','至少6个字符',{
        min: 6,
        max: 128
      })
    ]
  }
  
}
//校验token是否存在
class NotEmptyValidator extends LinValidator{
  constructor(){
    super()
    this.token = [
      new Rule('isLength','不允许为空',{
        min: 1
      })
    ]
  }
}
//新增栏目参数校验
class AddChannelValidator extends LinValidator{
  constructor(){
    super()
    this.channelName = [
      new Rule('isLength','名称不允许为空',{
        min: 1
      })
    ]
  }
}
//选择栏目参数校验
class setArticleChannelValidator extends LinValidator{
  constructor(){
    super()
    this.channelId = [
      new Rule('isLength','栏目ID不允许为空',{
        min: 1
      })
    ]
    this.ids = [
      new Rule('isLength','文章ID不允许为空',{
        min: 1
      })
    ]
  }
}
//新增文章参数校验
class AddArticleValidator extends LinValidator{
  constructor(){
    super()
    this.title = [
      new Rule('isLength','不允许为空',{
        min: 1
      })
    ]
    this.channelId = [
      new Rule('isLength','ID不允许为空',{
        min: 1
      })
    ]
    this.content = [
      new Rule('isLength','不允许为空',{
        min: 1
      })
    ]
  }
}



module.exports = {
  PositiveIntegerValidator,
  RegisterValidator,
  LoginValidator,
  NotEmptyValidator,
  AddChannelValidator,
  AddArticleValidator,
  setArticleChannelValidator
}